#ifndef SCROBBLER_H_
#define SCROBBLER_H_

#include <string>
#include <ctime>
#include <deque>
#include <fstream>
#include "fileinfo.h"

struct url
{
	std::string host;
	uint32_t hostIP;
	std::string path;
	int port;
};

struct scrobbl
{
	fileInfo info;
	time_t timestamp;
};

class Scrobbler
{
	public:
		Scrobbler();
		void readCfg();
		bool toggleOnline();
		void nowplaying(fileInfo *tuneInfo);
		void submission(fileInfo *tuneInfo, time_t *timestamp);
		void process();
		void writeScrobbls();
		void readScrobbls();
		std::string getMessage();
	
	private:
		bool online;
		std::fstream fileHandler;
		bool irqIsRunning;
		bool scrobblerOk;
		url serverURL;
		std::string username;
		std::string password;
		std::string sessionID;
		std::string message;
		
		fileInfo nowQueue;
		bool nowQueueSent;
		url nowURL;
		std::deque<scrobbl> submitQueue;
		url submitURL;
		
		bool connecTry();
		int establish(url* server);
		std::string sendrecv(int socket, std::string request);

		bool handshake();
		void submitProcess();
		void nowProcess();
		
		std::string makeHandshakeRequest();
		std::string makeNowRequest(fileInfo tuneInfo);
		std::string makeSubmitRequest();
		
		url urlParser(std::string url);
		std::string urlencode(unsigned char *str);
};

#endif /* SCROBBLER_H_ */
